home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / testcpp / 11 / test.g < prev   
Encoding:
Text File  |  1994-09-14  |  1.4 KB  |  84 lines  |  [TEXT/MPS ]

  1. /* This is test.g which tests multiple scanners/parsers; DLG-based scanner;
  2.  * also, we test multiple lexical classes.
  3.  */
  4. <<
  5. #include "Lexer.h"
  6. typedef ANTLRCommonToken ANTLRToken;
  7.  
  8. main()
  9. {
  10.     ANTLRToken aToken;
  11.     DLGFileInput in(stdin);
  12.     Lexer scan(&in,2000);
  13.     ANTLRTokenBuffer pipe(&scan);
  14.     scan.setToken(&aToken);
  15.     Include parser(&pipe);
  16.     parser.init();
  17.  
  18.     parser.input();
  19. }
  20. >>
  21.  
  22. #token "[\ \t\n]+"    <<skip();>>
  23.  
  24. #lexclass START
  25.  
  26. class Include {
  27.  
  28. <<
  29. /* this is automatically defined to be a member function of Include::
  30.  * since it is within the "class {...}" boundaries.
  31.  */
  32. private:
  33. char *stripquotes(ANTLRChar *s)
  34. {
  35.     s[strlen(s)-1] = '\0';
  36.     return &s[1];
  37. }
  38. >>
  39.  
  40. input
  41.     :    ( cmd | include )* "@"
  42.     ;
  43.  
  44. cmd    :    "print"
  45.         (    NUMBER        <<printf("%s\n", $1->getText());>>
  46.         |    STRING        <<printf("%s\n", $1->getText());>>
  47.         )
  48.     ;
  49.  
  50. include
  51.     :    "#data" STRING
  52.         <<{
  53.         FILE *f;
  54.         f = fopen(stripquotes($2->getText()), "r");
  55.         if ( f==NULL ) {fprintf(stderr, "can't open %s\n", $2->getText()+1);}
  56.         else {
  57.             ANTLRToken aToken;
  58.             DLGFileInput in(f);
  59.             Lexer scan(&in);
  60.             scan.setToken(&aToken);
  61.             scan.mode(Lexer::DATA);
  62.             ANTLRTokenBuffer pipe(&scan);
  63.             Include parser(&pipe);
  64.             parser.init();
  65.             parser.data();
  66.         }
  67.         }>>
  68.     ;
  69.  
  70. #lexclass DATA
  71.  
  72. #token "[\ \t\n]+"    <<skip();>>
  73.  
  74. data:    "0x[0-9]+" ":" "0x[0-9]+"
  75.         <<printf("data %s\n", $1->getText());>>
  76.     ;
  77.  
  78. }
  79.  
  80. #lexclass START
  81.  
  82. #token STRING    "\" [a-zA-Z0-9_.,\ \t]+ \""
  83. #token NUMBER    "[0-9]+"
  84.